package com.liyu.rabbit.roll;

import com.liyu.rabbit.Producer;
import com.liyu.rabbit.factory.RabbitFactory;
import com.rabbitmq.client.Channel;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 作者: 李誉
 * 时间: 2022-03-03 10:40
 * 描述: 轮询消费者
 */
public class Consumer {

    //消费消息
    public static void main(String[] args) {

        // 创建线程池
        ExecutorService cacheThreadPool = Executors.newCachedThreadPool();

        for(int i = 0; i < 2; i++){
            final int index = i ;
            cacheThreadPool.execute(() -> {
                try {
                    Channel channel = RabbitFactory.createNewChannel();
                    channel.basicConsume(Producer.QUEUE_NAME, true, (consumerTag, message) -> {
                        System.out.println("线程 -"+ index + "- 消费一条消息成功：" + new String(message.getBody()));
                    }, (consumerTag) -> {
                        System.out.println("线程 -"+ index + "- 消费一条消息失败：");
                    });
                    System.out.println("线程 -"+ index + "- 启动成功。。。。。。。。。。");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }



    }
}
